package com.xiaoyu.commonlib.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.xiaoyu.commonlib.CommonPlug;

public abstract class PreferencesUtil {

    private static SharedPreferences mSharePreferences;
    private static final String CONFIG_FILE_NAME = "share_config";

    public static final String UID = "uid";//极光推送的uid(用这个做别名)
    public static final String TOKEN = "token";//唯一标识token
    public static final String LOGIN_TYPE = "login_type";//登录方式 1：账号密码登录 2：第三方登录
    public static final String USERNAME = "login_username";//登录账号
    public static final String PASSWORD = "login_password";//登录密码
    public static final String KEY_HAS_GUIDED = "KEY_HAS_GUIDED"; //是否已经完成引导
    public static final String IS_SIGN_IN = "IS_SIGN_IN"; //是否已经签到
    public static final String OPEN_MESSAGE = "OPEN_MESSAGE"; //打开消息提醒
    public static final String TITLE_HEAD = "TITLE_HEAD"; //保存头像
    public static final String STORESTATE = "STORESTATE"; //是否申请开店成功
    public static final String PAY_PWD = "PAY_PWD"; //支付密码


    private synchronized static SharedPreferences preferences() {
        if (mSharePreferences == null) {
            mSharePreferences = CommonPlug.getAppContext()
                    .getSharedPreferences(CONFIG_FILE_NAME,
                            Context.MODE_PRIVATE);
        }
        return mSharePreferences;
    }

    public static String getString(String key, String defValue) {
        return preferences().getString(key, defValue);
    }

    public static int getInt(String key, int defValue) {
        return preferences().getInt(key, defValue);
    }

    public static long getLong(String key, long defValue) {
        return preferences().getLong(key, defValue);
    }

    public static boolean getBoolean(String key, boolean defValue) {
        return preferences().getBoolean(key, defValue);
    }

    public static boolean commit(String key, int value) {
        return preferences().edit().putInt(key, value).commit();
    }

    public static boolean commit(String key, String value) {
        return preferences().edit().putString(key, value).commit();
    }

    public static boolean commit(String key, long value) {
        return preferences().edit().putLong(key, value).commit();
    }

    public static boolean commit(String key, Boolean value) {
        return preferences().edit().putBoolean(key, value).commit();
    }

    public static boolean commitString(String key, String value, int count) {
        if (count == -1) {
            preferences().edit().putString(key, value).commit();
        }

        if (!TextUtils.isEmpty(preferences().getString(key, null))) {
            String perenceValue = preferences().getString(key, null);
            if (perenceValue.contains(value)) {
                if (perenceValue.contains(",")) {
                    preferences().edit().putString(key, "");
                    for (int i = 0; i < perenceValue.split(",").length; i++) {
                        if (value.equals(perenceValue.split(",")[i])) {

                        } else {
                            if (TextUtils.isEmpty(preferences().getString(key, null))) {
                                preferences().edit().putString(key, perenceValue.split(",")[i]);
                            } else {
                                preferences().edit().putString(key, preferences().getString(key, null) + "," + perenceValue.split(",")[i]);
                            }

                        }
                    }
                } else {
                    return preferences().edit().putString(key, value).commit();
                }
            }
            if (perenceValue.contains(",")) {
                if (perenceValue.split(",").length >= count)
                    return preferences().edit().putString(key, value + "," + (preferences().getString(key, null).substring(0, preferences().getString(key, null).lastIndexOf(",")))).commit();
                else
                    return preferences().edit().putString(key, value + "," + preferences().getString(key, null)).commit();
            } else
                return preferences().edit().putString(key, value + "," + preferences().getString(key, null)).commit();
        } else
            return preferences().edit().putString(key, value).commit();


    }

    //清空数据（正确，只是把表中所有数据清空了）
    public static void clear(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(CONFIG_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();

        editor.commit();
    }

    public static void clear() {
        PreferencesUtil.commit(PreferencesUtil.TOKEN, "");
        PreferencesUtil.commit(PreferencesUtil.IS_SIGN_IN, "");
        PreferencesUtil.commit(PreferencesUtil.LOGIN_TYPE, 0);
        PreferencesUtil.commit(PreferencesUtil.USERNAME, "");
        PreferencesUtil.commit(PreferencesUtil.PASSWORD, "");
        PreferencesUtil.commit(PreferencesUtil.OPEN_MESSAGE, false);
        PreferencesUtil.commit(PreferencesUtil.TITLE_HEAD, "");
        PreferencesUtil.commit(PreferencesUtil.UID, "");
        PreferencesUtil.commit(PreferencesUtil.STORESTATE, "");
        PreferencesUtil.commit(PreferencesUtil.PAY_PWD, "");
    }

}
